package cs304.p3.library.controller;

import java.util.List;

import cs304.p3.library.domain.Borrower;
import cs304.p3.library.domain.Clerk;
import cs304.p3.library.exceptions.AbstractLDBException;
import cs304.p3.library.exceptions.UnavailableBookException;
import cs304.p3.library.framework.IoC;
import cs304.p3.library.service.ClerkService;
import cs304.p3.library.view.ClerkDialog;

public class ClerkController extends Controller{
	
	private Borrower borrower = new Borrower();
	private Clerk clerk = new Clerk();
	private ClerkDialog view;
	
	public void setView(ClerkDialog v){
		view = v;
	}
	
	public ClerkService getService(){
		return clerkService;
	}

	@Override
	public void cleanUp() {
		borrower = new Borrower();
		clerk = new Clerk();
	}

	public void updateBookList() {
		view.updateBookList(clerk.getCheckOutBook());
	}

	public void performAddBorrowerAction() {
		boolean error = false;
		Borrower newBorrower = null;
		try {
			newBorrower = clerkService.addBorrower(borrower.getName(), borrower.getAddress(), borrower.getPhone(), 
					borrower.getEmailAddress(), borrower.getSinOrStNo(), 
					borrower.getExpiryDate(), borrower.getType());
		} catch (AbstractLDBException e) {
			System.err.println("Could not add Borrower");
			e.printStackTrace();
			e.displayError();
			error = true;
		}
		if(!error){
			IoC.displayCompleteMessage("Borrower Added");
			IoC.displayCompleteMessage("BID: " + newBorrower.getBid() + "\nPassword: " + newBorrower.getPassword());
			view.clearAddBorrowerFields();
		}
	}

	public Clerk getClerk() {
		return clerk;
	}

	public Borrower getBorrower() {
		return borrower;
	}

	public void performCheckOutAction() {
		List<String> books = clerk.getCheckOutBooks();
		ClerkService service = IoC.resolve(ClerkService.class);
		boolean error = false;
		try {
			if(clerk.getBID().isEmpty()){
				IoC.displayUserInvalidInputMessage("BID cannot be empty");
				error = true;
			}else service.checkOut(clerk.getBID(), books);
		} catch(UnavailableBookException e) {
			System.err.println("Unavailable call numbers: ");
			for(String callNumber:e.getCallNumbers()) {
				System.err.print(callNumber);
			}
			System.err.println();
			
			error = true;
			e.displayError();
		} catch(AbstractLDBException e){
			error = true;
			e.printStackTrace();
			e.displayError();
		}
		if(!error) {
			IoC.displayCompleteMessage("Item Checked Out");
			view.clearCheckOutFields();
		}
	}

	public void performReturnAction() {
		ClerkService service = IoC.resolve(ClerkService.class);
		boolean error = false;
		try {
			if(clerk.getReturnCopyNumber().isEmpty()){
				IoC.displayUserInvalidInputMessage("Copy Number cannot be empty");
				error = true;
			}else if(clerk.getReturnCallNumber().isEmpty()){
				IoC.displayUserInvalidInputMessage("Call Number cannot be empty");
				error = true;
			}else service.processReturn(clerk.getReturnCallNumber(), Integer.parseInt(clerk.getReturnCopyNumber()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			error = true;
		}catch(AbstractLDBException e){
			e.printStackTrace();
			e.displayError();
			error = true;
		}
		if(!error){
			view.clearReturnFields();
			IoC.displayCompleteMessage("Item Returned");
		}
	}

	public void performOverdueRefreshAction() {
		clerk.setOverdues(clerkService.checkOverdue());
	}

}
